package com.lsl.im.dao;

import java.util.ArrayList;
import java.util.List;

import com.lsl.im.common.DBOpenHelper;
import com.lsl.im.po.FriendRequestRecord;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class FriendRequestDao {
	
	private Context context;
	private DBOpenHelper dbOpenHelper;
	
	public FriendRequestDao(){}
	public FriendRequestDao(Context context){
		this.context = context;
		dbOpenHelper = new DBOpenHelper(context);
	}
	
	public void insertCollect(FriendRequestRecord record) throws Exception {					
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		try{
			db.beginTransaction();
			
			db.execSQL("insert into friendrequest(fromjid,receivedate,isfriend,type) values(?,?,?,?)", 
					new Object[]{record.getFromJID(),record.getReceiveDate(),record.getIsFriend(),record.getType()});
						
			db.setTransactionSuccessful();
		}catch(Exception e){
			throw e;
		}finally{
			db.endTransaction();
		}
	}
	
	
	public List<FriendRequestRecord> getFriendRequestRecordList() throws Exception{
		List<FriendRequestRecord> list = new ArrayList<FriendRequestRecord>();
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("select id,fromjid,receivedate,isfriend,type from friendrequest order by id desc",null);
		while(cursor.moveToNext()){
			FriendRequestRecord record = new FriendRequestRecord();
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String fromJID = cursor.getString(cursor.getColumnIndex("fromjid"));
			String receiveDate = cursor.getString(cursor.getColumnIndex("receivedate"));
			String isFriend = cursor.getString(cursor.getColumnIndex("isfriend"));
			String type = cursor.getString(cursor.getColumnIndex("type"));
			
			record.setId(id);
			record.setFromJID(fromJID);
			record.setReceiveDate(receiveDate);
			record.setIsFriend(isFriend);
			record.setType(type);
			
			list.add(record);
		}
		cursor.close();
		return list;
	}
	
	/*public List<Product> getCollectProductList() throws Exception{
		List<Product> list = new ArrayList<Product>();
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("select id,name,price,pop,imageurl,descurl from product order by id desc",null);
		while(cursor.moveToNext()){
			Product p = new Product();
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String price = cursor.getString(cursor.getColumnIndex("price"));
			String pop = cursor.getString(cursor.getColumnIndex("pop"));
			String imageurl = cursor.getString(cursor.getColumnIndex("imageurl"));
			String descurl = cursor.getString(cursor.getColumnIndex("descurl"));
			p.setId(id);
			p.setName(name);
			p.setPrice(price);
			p.setPop(pop);
			p.setImageUrl(imageurl);
			p.setDescUrl(descurl);
			list.add(p);
		}
		cursor.close();
		return list;
	}
	
	public Product getCollectProductByPop(String popArg) throws Exception{
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		Cursor cursor;
		cursor = db.rawQuery("select id,name,price,pop,imageurl,descurl from product where pop=?",new String[]{popArg});
		Product p = null;
		if(cursor.moveToNext()){
			p = new Product();
			int id = cursor.getInt(cursor.getColumnIndex("id"));
			String name = cursor.getString(cursor.getColumnIndex("name"));
			String price = cursor.getString(cursor.getColumnIndex("price"));
			String pop = cursor.getString(cursor.getColumnIndex("pop"));
			String imageurl = cursor.getString(cursor.getColumnIndex("imageurl"));
			String descurl = cursor.getString(cursor.getColumnIndex("descurl"));
			p.setId(id);
			p.setName(name);
			p.setPrice(price);
			p.setPop(pop);
			p.setImageUrl(imageurl);
			p.setDescUrl(descurl);
		}
		cursor.close();
		return p;
	}
	
	
	public void insertCollect(Product p) throws Exception {
		
		Product exitsP = getCollectProductByPop(p.getPop());
		if(exitsP!=null){
			return ;
		}
		
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		try{
			db.beginTransaction();
			
			db.execSQL("insert into product(name,price,pop,imageurl,descurl) values(?,?,?,?,?)", new Object[]{p.getName(),p.getPrice(),p.getPop(),p.getImageUrl(),p.getDescUrl()});
						
			db.setTransactionSuccessful();
		}catch(Exception e){
			throw e;
		}finally{
			db.endTransaction();
		}
	}
	
	
	public void deleteCollectById(int id) throws Exception {
		SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
		try{
			db.beginTransaction();			
			db.execSQL("delete from product where id=?", new Object[]{id});
			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
	}*/
	
	
}
